package com.ysd.Controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.entity.Modules;
import com.ysd.entity.Roles;
import com.ysd.entity.Users;
import com.ysd.service.ModulesService;
import com.ysd.service.RolesService;
import com.ysd.service.UserService;
import com.ysd.util.ResultInfoUtil;
import com.ysd.util.Treemodule;
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private ModulesService modulesService;
	@Autowired
	private RolesService rolesservice;
	@GetMapping
	public ResultInfoUtil findAllUsers(Integer limit,Integer page,Users u) {
		Pageable pageable = PageRequest.of(page-1, limit);//PageRequest.of
		Page<Users> use =  userService.findAllUsers(u, pageable);
		return new ResultInfoUtil().success(use.getContent(),use.getTotalElements());
	}
	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	@DeleteMapping
	public Integer deleteUser(Users u) {
		try {
			userService.deleteUser(u);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			return 2;
		}
		
	}
	/**
	 * 添加用户
	 * @param s
	 * @return
	 */
	@PostMapping
	public Users addUsers(Users s) {
		UUID.randomUUID().toString();
		s.setId(UUID.randomUUID().toString());
		s.setCreateTime(new Date());
		return userService.addAndUpdate(s);
		
	}
	/**
	 * 修改用户
	 * @param s
	 * @return
	 */
	@PutMapping
	public Users updateUsers(Users s) {
		return userService.addAndUpdate(s);
	}
	

	/**
	 * 根据用户id查看用户角色
	 */
	@GetMapping("/getuseridfindRoles")
	public List<Roles> findusers(String userid) {
		System.out.println("根据用户id查看用户角色"+userid);
		Optional<Users> u = userService.findByIDfindRoles(userid);
		List<Roles> d = u.get().getRoles();
		System.out.println("d.size()"+d.size());
		return d;

	}
	
	/**
	 * 给用户添加角色
	 * @param userid
	 * @param roleid
	 */
	@PostMapping("/AddUserToRole")
	public void AddUserToRole(String userid,String roleid){
		System.out.println(userid+"----------------------------");
		System.out.println(roleid+"----------------------------");
		rolesservice.saveUserrole(userid, roleid);
		
	}
	/**
	 * 给用户删除角色
	 * @param uId
	 * @param rId
	 */
	
	@RequestMapping("/delUserToRole")
	public void delUserToRole(String userid,String roleid){
		System.out.println(userid+"----------------------------");
		System.out.println(roleid+"----------------------------");
		rolesservice.delUserrole(userid, roleid);
		
	}
	
	/**
	 * 重置密码
	 */
	@RequestMapping("/uppassword")
	public Integer uppassword(String Id) {
		System.out.println(Id);
		return userService.uppassword(Id);
	}
	
	//---------------------------------------------------------------------
	/**
	 * 查询登录用户的模块信息并返回树型结构
	 * @param id
	 * @return
	 */
	@GetMapping("/modules") 
	public Map<Object, Object> findUserById(String Id){
		System.out.println(Id+"..........");
		
		Map<Object, Object> restMap = new HashMap<Object, Object>();
		Users user = userService.findUsersById(Id);
		// 获取该用户所有模块
		 // 创建模块集合
		List<Modules> moduleList = new ArrayList<Modules>();
		for(Roles r:user.getRoles()) {
			for(Modules m:r.getModules()) {  
				if(!moduleList.contains(m)) {
					moduleList.add(m);
				}
			}
		}
		// 获取该用户的模块树
		List<Treemodule> modulesTree = modulesService.findUserModules(moduleList);
		
		if(user.getLoginName()!=null) { 
			restMap.put("tree", modulesTree); 
			restMap.put("code", "0");
			restMap.put("message", "成功");
		}else {
			restMap.put("code", "1");
			restMap.put("message", "失败");
		}
		return restMap;
	}

}
